package pzl.media.dsp;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.MediaCodecInfo;
import android.media.MediaCodecList;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Gop是指多少秒一个I帧
 * <p>
 * https://developer.android.google.cn/reference/android/media/MediaCodec
 * https://www.jianshu.com/p/f5a1c9318524
 */
public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private final static int CAMERA_OK = 10001;
    private static String[] PERMISSIONS = {
            Manifest.permission.CAMERA,
            Manifest.permission.WRITE_EXTERNAL_STORAGE};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.tvMediaCodecMsg);

        if (!checkPermissionAllGranted(PERMISSIONS))
            Toast.makeText(this, "无权限", Toast.LENGTH_LONG).show();
    }


    public void click(View v) {
        textView.setText("");
        Intent i = new Intent();
        switch (v.getId()) {
            case R.id.btnMediaCodecSupport:
                getMediaCodecSupport();
                break;
            case R.id.btnReqPermission:
                reqPermission();
                break;
            case R.id.btnMediaCodecCameraEncode:
                i.setClass(this, H264EncodeCameraActivity.class);
                startActivity(i);
                break;
            case R.id.btnMediaCodecDecode:
                i.setClass(this, H264DecoderActivity.class);
                startActivity(i);
                break;
        }
    }


    /**
     * 获取设备支持的编解码器的名字、能力，以查找合适的编解码器。
     * 以OMX.google.开头的通常是软件编解码器。
     * 以OMX.[硬件厂商名称]开头的通常是硬件编解码器。
     */
    private void getMediaCodecSupport() {
        MediaCodecList list = new MediaCodecList(MediaCodecList.REGULAR_CODECS);
        MediaCodecInfo[] codecs = list.getCodecInfos();
        textView.append("Decoders :\n");
        for (MediaCodecInfo codec : codecs) {
            if (codec.isEncoder())
                continue;
            textView.append(codec.getName() + "\n");
        }
        textView.append("Encoders :\n");
        for (MediaCodecInfo codec : codecs) {
            if (codec.isEncoder())
                textView.append(codec.getName() + "\n");
        }
    }


    private void reqPermission() {
        ActivityCompat.requestPermissions(MainActivity.this,
                PERMISSIONS, CAMERA_OK);
    }

    private boolean checkPermissionAllGranted(String[] permissions) {
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

}
